{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b031e5b3-aded-4c0c-83bb-3418c80c3385",
   "metadata": {},
   "source": [
    "# 列表常用方法、操作"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "3d15f912-93c9-408d-a760-e5098eeb2788",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 1.0, '12ab', True, [1, 1.0, '1'], {1}, {1: 1.0}]\n"
     ]
    }
   ],
   "source": [
    "# 创建一个混合列表\n",
    "my_list = [1, 1.0, '12ab', True, [1, 1.0, '1'], {1}, {1:1.0}]\n",
    "print(my_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "75992259-a1d8-42e4-9e48-16968ca9e166",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 1.0, '123', True, [1, 1.0, '1'], {1}, {1: 1.0}]\n"
     ]
    }
   ],
   "source": [
    "# 修改某个元素\n",
    "my_list[2] = '123'\n",
    "print(my_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "04457602-f309-4678-8471-f516c0a02709",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 1.0, 'inserted', '123', True, [1, 1.0, '1'], {1}, {1: 1.0}]\n"
     ]
    }
   ],
   "source": [
    "# 在列表指定位置插入元素\n",
    "my_list.insert(2, 'inserted')\n",
    "print(my_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "689d864d-9889-44e9-9d5e-3a17c53a3d0f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 1.0, 'inserted', '123', True, [1, 1.0, '1'], {1}, {1: 1.0}, 'tail']\n"
     ]
    }
   ],
   "source": [
    "# 在列表尾部插入元素\n",
    "my_list.append('tail')\n",
    "print(my_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "02d76f77-4d37-4f9e-946c-84af09b80d3f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 1.0, 'inserted', '123', True, [1, 1.0, '1'], {1}, {1: 1.0}]\n"
     ]
    }
   ],
   "source": [
    "# 通过索引删除\n",
    "del my_list[-1]\n",
    "print(my_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "fb7e99f5-dd7c-4f32-a8a2-d79bb7f3f0a0",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 1.0, 'inserted', True, [1, 1.0, '1'], {1}, {1: 1.0}]\n"
     ]
    }
   ],
   "source": [
    "# 删除某个元素\n",
    "my_list.remove('123')\n",
    "print(my_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "e8aa98a0-eda8-4eca-ab65-b56e6dcf36b7",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "No\n"
     ]
    }
   ],
   "source": [
    "# 判断一个元素是否在列表中\n",
    "\n",
    "if '123' in my_list:\n",
    "    print(\"Yes\")\n",
    "else:\n",
    "    print(\"No\")\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "00672a10-728d-42f0-ab60-8fda33ebff24",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{1: 1.0}, {1}, [1, 1.0, '1'], True, 'inserted', 1.0, 1]\n"
     ]
    }
   ],
   "source": [
    "# 列表翻转\n",
    "my_list.reverse()\n",
    "print(my_list) "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "cc55d1b0-9e63-4bcd-8a9e-895db54ccfcc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "J_a_m_e_s\n"
     ]
    }
   ],
   "source": [
    "# 将列表用所有字符连接，连接符为下划线 _\n",
    "letters = ['J', 'a', 'm', 'e', 's']\n",
    "word = '_'.join(letters)\n",
    "print(word)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b3f12162",
   "metadata": {},
   "source": [
    "List的插入难道没有API嘛"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dd6c3f20",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 大小为 10 的数组已经装了 4 个元素\n",
    "arr = [0] * 10\n",
    "for i in range(4):\n",
    "    arr[i] = i\n",
    "\n",
    "# 在第 3 个位置插入元素 666\n",
    "# 需要把第 3 个位置及之后的元素都往后移动一位\n",
    "# 注意要倒着遍历数组中已有元素避免覆盖，不懂的话请看下方可视化面板\n",
    "for i in range(4, 2, -1):\n",
    "    arr[i] = arr[i - 1]\n",
    "\n",
    "# 现在第 3 个位置空出来了，可以插入新元素\n",
    "arr[2] = 666"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9192d48d",
   "metadata": {},
   "source": [
    "有这个API"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "0ce82bac",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Final List :  [123, 'xyz', 'zara', 2009, 'abc']\n"
     ]
    }
   ],
   "source": [
    "aList = [123, 'xyz', 'zara', 'abc']\n",
    "\n",
    "aList.insert( 3, 2009)\n",
    "\n",
    "print(\"Final List : \", aList)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "science39",
   "language": "python",
   "name": "science39"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
